package com.ash.controller.chart;

import com.ash.domain.Chart;
import com.ash.domain.Order;
import com.ash.domain.vo.ChartVo;
import com.ash.service.chart.ChartService;
import com.ash.service.order.OrderService;
import com.ash.util.JsonUtil;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author : Chen Chao
 * @encoding : UTF-8
 * @date : 2021/4/12 16:33
 * @file : project06.com.ash.controller.chart.ChartController.java
 * @software : IntelliJ IDEA
 * @since : 1.8
 */
@Controller
public class ChartController {
    @Resource
    ChartService chartService;
    @Resource
    OrderService orderService;
    @RequestMapping("/user/addChart")
    @ResponseBody
    public Integer addChart(Chart chart){
        return chartService.add(chart);
    }
    @RequestMapping("/user/chart")
    public String chart(){
        return "user/chart";
    }
    @RequestMapping("/user/chart/selectAll")
    @ResponseBody
    public String selectAll(@Nullable @RequestParam("uid")Integer uid,
                            @Nullable @RequestParam("page") Integer page,
                            @Nullable @RequestParam("limit") Integer limit,
                            @Nullable @RequestParam("order") String order
    ){
        List<ChartVo> chartVos = chartService.selectByUid(uid,page,limit,order);
        return JsonUtil.getJsonList(chartService.selectDataCount(uid),chartVos);
    }
    @RequestMapping("/user/chart/deleteByIds")
    @ResponseBody
    public Integer deleteByIds(@RequestParam("ids")List<Integer> ids,@RequestParam("uid")Integer uid){
        return chartService.deleteByBids(uid,ids);
    }
    @RequestMapping("/user/chart/deleteById")
    @ResponseBody
    public Integer deleteById(@RequestParam("bid")Integer bid,@RequestParam("uid")Integer uid){
        return chartService.deleteByBid(uid,bid);
    }
    @RequestMapping("/user/chart/setCount")
    @ResponseBody
    public Integer setCount(Chart chart){
        return chartService.setCount(chart);
    }
    @PostMapping("/user/chart/settleAccount")
    @ResponseBody
    public BigDecimal settleAccount(@RequestParam("ids")List<Integer> ids, @RequestParam("uid")Integer uid){

        return chartService.settleAccount(ids,uid);
    }
    @RequestMapping("/user/chart/createOrderById")
    @ResponseBody
    public Integer createOrderById(@RequestParam("ids")List<Integer> ids, @RequestParam("uid")Integer uid){
        /*创建订单*/
        Order order = new Order();
        order.setUid(uid);
        order.setCreateDate(new Date());
        orderService.add(order);
        /*添加订单信息*/
        List<ChartVo> chartVos = chartService.selectByUidAndBids(ids, uid);
        orderService.addDetail(chartVos,order.getOid());
        /*删除购物车*/
        chartService.deleteByBids(uid,ids);
        return 1;
    }
}
